package com.lizemin.mybatis.sqlHandler;

import com.lizemin.mybatis.entity.DeleteSql;
import com.lizemin.mybatis.entity.MapperSql;
import com.lizemin.mybatis.entity.ParamValue;
import com.lizemin.mybatis.util.SqlUtil;

import javax.sql.DataSource;
import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;

/**
 * @author lzm
 * @date 2025/5/20 22:10
 * @description
 */
public class DeleteSqlHandler extends SqlHandler {

    public DeleteSqlHandler(DataSource dataSource) {
        super(dataSource);
    }

    @Override
    public Object handle(MapperSql mapperSql, HashMap<String, ParamValue> paramValueMap, Class<?> returnType) {
        DeleteSql deleteSql = (DeleteSql) mapperSql;
        if (paramValueMap.isEmpty()) {
            throw new RuntimeException("参数不能为空");
        }
        String sql = deleteSql.getSql();
        List<String> placeholders = SqlUtil.getPlaceholders(sql);
        sql = sql.replaceAll("#\\{[^}]+}", "?");
        try (Connection conn = dataSource.getConnection();
             // 使用 PreparedStatement 预编译 SQL 语句，防止 SQL 注入，且 SQL 来源安全
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            for (int i = 0; i < placeholders.size(); i++) {
                String placeholder = placeholders.get(i);
                if (placeholder.contains(".")) {
                    String[] strArr = placeholder.split("\\.");
                    String paramName = strArr[0];
                    String paramFieldName = strArr[1];
                    ParamValue paramValue = paramValueMap.get(paramName);
                    Object obj = paramValue.getValue();
                    Field filed = paramValue.getType().getDeclaredField(paramFieldName);
                    filed.setAccessible(true);
                    Object value = filed.get(obj);
                    Class<?> type = filed.getType();
                    if (type.equals(String.class)) {
                        pstmt.setString(i + 1, (String) value);
                    } else if (type.equals(Integer.class) || type.equals(int.class)) {
                        pstmt.setInt(i + 1, (int) value);
                    } else if (type.equals(Long.class) || type.equals(long.class)) {
                        pstmt.setLong(i + 1, (long) value);
                    }
                } else {
                    ParamValue paramValue = paramValueMap.get(placeholder);
                    Class<?> type = paramValue.getType();
                    if (type.equals(String.class)) {
                        pstmt.setString(i + 1, (String) paramValue.getValue());
                    } else if (type.equals(Integer.class) || type.equals(int.class)) {
                        pstmt.setInt(i + 1, (int) paramValue.getValue());
                    } else if (type.equals(Long.class) || type.equals(long.class)) {
                        pstmt.setLong(i + 1, (long) paramValue.getValue());
                    }
                }
            }
            int rows = pstmt.executeUpdate();
            if (returnType.equals(Integer.class) || returnType.equals(int.class)) {
                return rows;
            }
            if (returnType.equals(long.class) || returnType.equals(Long.class)) {
                return (long) rows;
            }
        } catch (SQLException | NoSuchFieldException | IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }
}
